import { defineStore } from "pinia";
import { UserLoginInfo, UserInfoDetails } from "@/types";
import { removeLocalStorage } from "@/utils";
import { LocalstorageEnum } from "@/types/localstorage.ts";
import { HomeAxios } from "@/api/home.ts";
interface UserState {
  userLoginInfo: UserLoginInfo;
  userInfoDetails: UserInfoDetails;
  balance: number;
  withdrawDama: number;
  getNewToKenType: boolean;
}
export const getDefaultUserConfig = () => {
  return {
    account: {
      uid: 0,
      accountState: 2, //账户状态 2-正常 4-封禁 10-注销
      username: "",
      vipLevel: 0,
      linkId: "",
      uniqid: "",
    },
    profile: {
      email: "",
      avatar: "",
      language: 0,
      phoneNo: 0,
      areaNo: 0,
      name: "",
      cpf: "",
      cpfName: "",
    },
    notifyMsgStat: {
      notRead: 0,
      total: 0,
    },
    token: {
      accessToken: "",
      expiresIn: 0,
      refreshExpiresIn: 0,
      refreshToken: "",
    },
  };
};
export const getUserInfoDetails = () => {
  return {
    account: {
      account: "",
      accountState: 0, //账户状态 2-正常 4-封禁 10-注销
      accountType: 0, //2-账号密码登录、3-验证码 4-谷歌、6-FB
      channelId: 0,
      createTime: "",
      uid: 0,
      updateTime: "",
      upperId: 0,
      vipLevel: 0,
      linkId: "",
      level: 0,
    },
    profile: {
      areaNo: 0,
      avatar: "",
      email: "",
      language: 0,
      phoneNo: 0,
      name: "",
      cpf: "",
      cpfName: "",
    },
  };
};

export const UserStore = defineStore("userStore", {
  state: (): UserState => ({
    userLoginInfo: getDefaultUserConfig(),
    userInfoDetails: getUserInfoDetails(),
    balance: 0, //账户余额
    withdrawDama: 0, //打码量
    getNewToKenType: false, //是否正在加载替换新的token
  }),
  getters: {
    isLogin(state) {
      return !!state.userLoginInfo.account.uid;
    },
  },
  actions: {
    setState(data: Partial<UserState>) {
      Object.assign(this, data);
    },
    setUserConfig(data: Partial<UserState>) {
      Object.assign(this, data);
    },
    //用户登录详情
    setUserLoginInfo(data: Partial<UserLoginInfo>) {
      if (data?.profile?.name === "") {
        data.profile.name = data?.profile?.name === "" ? "10086" : data?.profile?.name;
      }
      Object.assign(this.userLoginInfo, data);
      const uniqid: any = data?.account?.uniqid;
      localStorage.setItem("deviceUniqueKey", uniqid);
    },
    //用户信息详情
    setUserInfoDetails(data: Partial<UserInfoDetails>) {
      if (data?.profile?.name === "") {
        data.profile.name = data?.profile?.name === "" ? "10086" : data?.profile?.name;
      }
      Object.assign(this.userInfoDetails, data);
    },
    //重新设置用户的token
    setNewToken(data: { accessToken: string; expiresIn: number }) {
      Object.assign(this.userLoginInfo.token, data);
    },
    //重置用户信息
    resetUserConfig() {
      this.setUserLoginInfo(getDefaultUserConfig());
      this.setUserInfoDetails(getUserInfoDetails());
    },

    async resetDeviceConfig() {
      removeLocalStorage(LocalstorageEnum.BROWSER_ID);
    },
    //修改替换token的状态
    setNewToKenType() {
      this.getNewToKenType = this.getNewToKenType ? false : true;
    },
    //查询账号余额
    async getBalance() {
      const queryData: any = { providerId: 0, gameId: 0, code: 0 };
      await HomeAxios.queryBalance().then(e => {
        queryData.code = e.code;
        queryData.providerId = e.providerId;
        queryData.gameId = e.gameId;
        this.balance = e.balance;
        this.withdrawDama = e.withdrawDama;
      });
      return queryData;
    },
  },
  //本地持久化
  persist: true,
});
